package com.camel.chart.utils;

import cn.hutool.core.util.ObjectUtil;
import com.camel.chart.utils.entity.ChartNetWorkInterface;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class NetInterfaceUtil {

    public static List<ChartNetWorkInterface> getNetInterfaces() throws SocketException {
        ArrayList<ChartNetWorkInterface> res = new ArrayList<>();
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface net = networkInterfaces.nextElement();
            if (net.isUp()) {
                ChartNetWorkInterface in = new ChartNetWorkInterface();
                if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                    byte[] mac = net.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (byte b : mac) {
                            sb.append(String.format("%02X:", b));
                        }
                        // 去除最后一个冒号
                        String macAddress = sb.deleteCharAt(sb.length() - 1).toString();
                        System.out.println("MAC Address: " + macAddress);
                        in.setDisPlayName(net.getDisplayName());
                        in.setInetAddress(macAddress);
                        for (InterfaceAddress interfaceAddress : net.getInterfaceAddresses()) {
                            InetAddress broadcast = interfaceAddress.getBroadcast();
                            if (!ObjectUtil.isEmpty(broadcast)) {
                                in.setBroadcastAddress(broadcast.getHostAddress());
                            }
                        }
                    }
                } else {
                    System.out.println("Network Interface: " + net.getName());
                    System.out.println("NEtwork DisplayName: " + net.getDisplayName());
                    System.out.println("NEtwork IsVirtual: " + net.isVirtual());
                    Enumeration<InetAddress> inetAddresses = net.getInetAddresses();
                    System.out.println("NEtwork Address: " + inetAddresses.nextElement().getHostAddress());
                    System.out.println("========================================");

                    in.setDisPlayName(net.getDisplayName());
                    in.setInetAddress(net.getInetAddresses().nextElement().getHostAddress());
                    for (InterfaceAddress interfaceAddress : net.getInterfaceAddresses()) {
                        InetAddress broadcast = interfaceAddress.getBroadcast();
                        if (!ObjectUtil.isEmpty(broadcast)) {
                            in.setBroadcastAddress(broadcast.getHostAddress());
                        }
                    }
                }

                res.add(in);
            }
        }
        return res;
    }
}
